package com.yutian.springbootwebflux.base;

import java.io.Serializable;

public class BaseResult<T> implements Serializable {

    private int code;
    private String message;
    private T result;
    private String traceNo;

    public BaseResult() {
    }

    public BaseResult(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public BaseResult(ResultCodeEnum resultCode) {
        this.code = resultCode.getCode();
        this.message = resultCode.getMsg();
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getResult() {
        return result;
    }

    public void setResult(T result) {
        this.result = result;
    }

    public String getTraceNo() {
        return traceNo;
    }

    public void setTraceNo(String traceNo) {
        this.traceNo = traceNo;
    }

    public static BaseResult success() {
        return success(null);
    }

    public static<T> BaseResult success(T result) {
        BaseResult res = new BaseResult(ResultCodeEnum.SUCCESS.getCode(), null);
        res.setResult(result);
        return res;
    }

    public static BaseResult error() {
        return error(ResultCodeEnum.SERVER_ERROR.getCode(), ResultCodeEnum.SERVER_ERROR.getMsg());
    }

    public static BaseResult error(String msg) {
        return error(ResultCodeEnum.SERVER_ERROR.getCode(), msg);
    }

    public static BaseResult error(ResultCodeEnum resultCodeEnum) {
        return error(resultCodeEnum.getCode(), resultCodeEnum.getMsg());
    }

    public static BaseResult error(int code, String msg) {
        return new BaseResult(code, msg);
    }

    @Override
    public String toString() {
        return "BaseResult{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", result=" + result +
                ", traceNo='" + traceNo + '\'' +
                '}';
    }
}
